function getElementsByClassName(node, classname) {
    var a = [];
    var re = new RegExp('(^| )'+classname+'( |$)');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function showWork()
{
	document.getElementById('inputTable').style.visibility = 'visible';	
	document.getElementById('li_work').className = "current";

	document.getElementById('features').style.visibility = 'collapse';
	document.getElementById('li_features').className = "";
}

function showFeatures()
{
	document.getElementById('features').style.visibility = 'visible';
	document.getElementById('li_features').className = "current";

	document.getElementById('inputTable').style.visibility = 'collapse';
	document.getElementById('li_work').className = "";
}

function getRequest() 
{
	var req = false;
	// most browsers
	try 
	{
		req = new XMLHttpRequest();
	}
	catch (e)
	{
		// IE
		try 
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			// try an older version
			try 
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return false;
			}
		}
	}
	return req;
}

function isEven(value) {
	if (value%2 == 0)
		return true;
	else
		return false;
}

function getRowClass(value)
{
	if (value)
		return "eventr";
	else
		return "oddtr";
}

function generateResultsTable(obj)
{
	var tbody = document.getElementById('winnerResultsTable');

	var keys = Object.keys(obj);
	var keyCount = keys.length;
	for(var index = 0; index < keyCount; index++)
	{
		var even = isEven(index);
		var rowClass = getRowClass(even);

		var key = keys[index];
		var trow = document.createElement('tr');
		trow.setAttribute("class", rowClass);

		var tcolumn = document.createElement('td');
		var span = document.createElement('span');
		span.innerHTML = key;
		tcolumn.appendChild(span);
		trow.appendChild(tcolumn);

		tcolumn = document.createElement('td');
		span = document.createElement('span');
		span.innerHTML = obj[key];
		tcolumn.appendChild(span);
		trow.appendChild(tcolumn);
		
		tbody.appendChild(trow);
	}
}

function getOutput() 
{
	var parameters = new Array();
	var inputElements = getElementsByClassName(document.body,'param');
	var length = inputElements.length;
	element = null;
	for (var i = 0; i < length; i++) {
		element = inputElements[i];
		var parameter = new Array();
		parameter[0] = element.name;
		parameter[1] = element.value;
		parameters[i] = parameter.join("=");
	}
	
	var formData = new FormData();
	
	var file = document.getElementById('file').files[0];
	formData.append('file', file);
	
	var file = document.getElementById('aptitude_file').files[0];
	formData.append('aptitude_file', file);
	
	var xhr = new XMLHttpRequest();
	var url = 'do_query.php?' + parameters.join("&");
	xhr.open("POST", url);
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) 
		{
			var return_data = xhr.responseText;
			document.getElementById('resultsTable').style.visibility = 'visible';
			var obj = JSON.parse(return_data);
			generateResultsTable(obj);
		}
		else
		{
			document.getElementById('resultsTable').style.visibility = 'visible';
		}
	}

	xhr.send(formData);
}

function validate_form2(form) {
	var errors = 0;
	var inputElements = getElementsByClassName(document.body, 'input');
	var length = inputElements.length;
	element = null;
	for (var i = 0; i < length; i++) {
		element = inputElements[i];
		var value = element.value;
		var visibility = 'collapse';
		if (!value){
			visibility = 'visible';
			errors = errors + 1;
		}
		var errorElementName = element.name + '_error';
		var errorElement = document.getElementById(errorElementName);
		errorElement.style.visibility = visibility;
	}

	if (errors == 0)
	{
		var processButton = document.getElementById('processButton');
		processButton.style.visibility = 'collapse';
		
		var inputTable = document.getElementById('inputTable');
		inputTable.style.visibility = 'collapse';
		inputTable.style.height = '0px';
		
		var features = document.getElementById('features');
		features.style.visibility = 'collapse';
		features.style.height = '0px';
		
		//window.alert('Todo OK!');
		
		getOutput();
	}
	
	// Do not reload page!!!
	return false;
}
		
function setStartUpValues(){
	setElementsVisibilityByElementsClassName('error', 'collapse');
	
	var resultsTable = document.getElementById('resultsTable');
	resultsTable.style.visibility = 'collapse';
	
	var resultsTable = document.getElementById('features');
	resultsTable.style.visibility = 'collapse';
}

function setElementVisibility(element, visibility){
	element.style.visibility = visibility;
}
		
function setElementsVisibilityByElementId(id, visibility){
	var element = document.getElementById(id);
	setElementVisibility(element, visibility);
}

function setElementsVisibilityByElementsClassName(className, visibility){
	var errorElements = getElementsByClassName(document.body, className);
	var length = errorElements.length;
	element = null;
	for (var i = 0; i < length; i++) {
		element = errorElements[i];
		setElementVisibility(element, visibility);
	}
}

function resetFile(fileName)
{
	var file = document.getElementById(fileName);
    var newInput = document.createElement("input"); 
    newInput.type = "file"; 
    newInput.id = file.id; 
    newInput.name = file.name; 
    newInput.className = file.className; 
    newInput.style.cssText = file.style.cssText; 
    file.parentNode.replaceChild(newInput, file); 
}

function openGenerationEvolutionWindow(){
	// muestra la url con los parametros...
	var url = '/robots/tp2/generationevolution.html';
	var height = $(window).height();
	var width = $(window).width();
	window.open(url,'winname','directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height);
}

var curGraphIndex = 0;
function showgraph(genindex)
{
	var oldGraphId = "gen_img_" + curGraphIndex;
	setElementsVisibilityByElementId(oldGraphId, 'collapse');

	var newGraphId = "gen_img_" + genindex;
	setElementsVisibilityByElementId(newGraphId, 'visible');
	curGraphIndex = genindex;
}

function goBack(){
	resetFile('file');
	resetFile('aptitude_file');
	
	var button = document.getElementById('processButton');
    var newbutton = document.createElement("input"); 
    newbutton.type = "submit"; 
    newbutton.id = button.id; 
	newbutton.value = button.value;
    button.parentNode.replaceChild(newbutton, button); 

	setElementsVisibilityByElementId('inputTable', 'visible');
	var inputTable = document.getElementById('inputTable');
	inputTable.style.height = 'auto';
	
	var features = document.getElementById('features');
	features.style.height = 'auto';
	
	setElementsVisibilityByElementId('resultsTable', 'collapse');
}

function openHelp()
{
	var url = '/robots/tp2/help.html';
	window.open(url,'winname','directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=640,height=480');
}